package esi.t2no1.ws.interceptor;

import static esi.t2no1.util.WsUtil.extractLogMessageParts;

import java.util.Map;
import java.util.logging.Logger;

import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.message.Message;
import org.springframework.beans.factory.annotation.Autowired;

import esi.t2no1.dao.message.MessageDAO;

/**
 * Interceptor that logs all outbound SOAP messages to database
 * 
 * @author Kaarel Jõgeva
 */
public class DbLoggingOutInterceptor extends LoggingOutInterceptor {

	private MessageDAO messageDAO;

	@Override
	public void handleFault(Message message) {
		handleMessage(message);
	}

	@Override
	protected void log(Logger logger, String message) {
		Map<String, Object> parts = extractLogMessageParts(message);
		parts.put("inbound", false);
		messageDAO.logMessage(parts);
	}

	@Autowired
	public void setMessageDAO(MessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}

}
